import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

typedef PickCallBack = Function(int index);

class CustomPick extends StatelessWidget {
  const CustomPick({
    super.key,
    required this.pickList,
    required this.confirmCallBack,
    this.cancelCallBack,
    this.index = 0,
  });

  //确认回调
  final PickCallBack confirmCallBack;

  //取消回调
  final PickCallBack? cancelCallBack;

  //数据list
  final List<String> pickList;

  //索引
  final int index;

  @override
  Widget build(BuildContext context) {
    FixedExtentScrollController scrollController =
        FixedExtentScrollController(initialItem: index);
    int currentIndex = index;
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(24), topRight: Radius.circular(24)),
      ),
      child: SizedBox(
        height: 600.h,
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            //取消文本
            Positioned(
                left: 40.w,
                top: 24.h,
                child: InkWell(
                  onTap: () {
                    cancelCallBack?.call(currentIndex) ?? Get.back();
                  },
                  child: Text(
                    "取消",
                    style: TextStyle(fontSize: 32.sp),
                  ),
                )),

            //确认文本
            Positioned(
                right: 40.w,
                top: 24.h,
                child: InkWell(
                  onTap: () {
                    confirmCallBack(currentIndex);
                    Get.back();
                  },
                  child: Text(
                    "确认",
                    style: TextStyle(fontSize: 32.sp),
                  ),
                )),

            //列表
            Positioned(
                top: 68.h,
                child: Row(
                  children: [
                    SizedBox(
                      height: 508.h,
                      width: 662.w,
                      child: CupertinoPicker(
                        itemExtent: 96.h,
                        diameterRatio: 1.0,
                        scrollController: scrollController,
                        selectionOverlay: _buildMagnifierScreen(),
                        onSelectedItemChanged: (int position) {
                          currentIndex = position;
                        },
                        children:
                            pickList.map((year) => _customText(year)).toList(),
                      ),
                    ),
                  ],
                )),
          ],
        ),
      ),
    );
  }

  Widget _customText(String content) {
    return Container(
      alignment: Alignment.center,
      child: Text(
        content,
        style: TextStyle(fontSize: 36.sp, color: Colors.black),
      ),
    );
  }

  Widget _buildMagnifierScreen() {
    const Color resolvedBorderColor = Color(0xFFD0D0D0);

    return IgnorePointer(
      child: Center(
        child: Container(
          decoration: const BoxDecoration(
            border: Border(
              top: BorderSide(width: 0.0, color: resolvedBorderColor),
              bottom: BorderSide(width: 0.0, color: resolvedBorderColor),
            ),
          ),
          constraints: BoxConstraints.expand(
            height: 96.h,
          ),
        ),
      ),
    );
  }
}
